/*
 *  Class name: TeamManagedBean.java
 *  Version: 1.0
 *  Date: 26.2.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.managedbeans.entity;

import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.TeamDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.TeamDetailDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.services.TeamServiceBeanLocal;
import cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.ActionState;
import cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMapper;
import cz.muni.fi.pa165.soccergamesarchive.managedbeans.application.NavigationMenuBean;
import cz.muni.fi.pa165.soccergamesarchive.managedbeans.entity.handlers.table.TeamArrangeableTableHandler;
import java.io.Serializable;
import java.util.Objects;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import static cz.muni.fi.pa165.soccergamesarchive.utils.LabelTextHandler.getActionDescriptionText;
import static cz.muni.fi.pa165.soccergamesarchive.utils.LabelTextHandler.getFullActionDescriptionText;
import static java.lang.Long.valueOf;

/**
 *
 * @author Michal Ďuriš
 */
@Named(value = "teamManagedBean")
@SessionScoped
public class TeamManagedBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @EJB
    private TeamServiceBeanLocal teamServiceBean;

    @Inject
    private TeamArrangeableTableHandler tableHandler;

    @Inject
    private NavigationMenuBean menuBean;

    private TeamDTO currentlyManagedTeam;

    private TeamDetailDTO currentlyDetailedTeam;

    private ActionState currentState;

    /**
     * Creates a new instance of TeamManagedBean
     */
    public TeamManagedBean() {
    }

    @PostConstruct
    public void init() {

        currentlyManagedTeam = new TeamDTO();
        currentlyDetailedTeam = null;

        currentState = ActionState.NONE;
    }

    public TeamDTO getCurrentlyManagedTeam() {
        return currentlyManagedTeam;
    }

    public void setCurrentlyManagedTeam(TeamDTO currentlyManagedTeam) {
        this.currentlyManagedTeam = currentlyManagedTeam;
    }

    public TeamDetailDTO getCurrentlyDetailedTeam() {
        return currentlyDetailedTeam;
    }

    public void setCurrentlyDetailedTeam(TeamDetailDTO currentlyDetailedTeam) {
        this.currentlyDetailedTeam = currentlyDetailedTeam;
    }

    public ActionState getCurrentState() {
        return currentState;
    }

    public void setCurrentState(ActionState currentState) {
        this.currentState = currentState;
    }

    public TeamArrangeableTableHandler getTableHandler() {
        return tableHandler;
    }

    public String getEditorPanelTitle() {

        String title;

        try {
            title = getFullActionDescriptionText(currentState,
                    NavigationMapper.TEAM_MODULE);
        } catch (IllegalStateException e) {
            title = "ERROR";
            //TODO: SOME ERROR STATE DIALOG WINDOW
        }

        return title;
    }

    public String getEditorButtonLabel() {

        String label;

        try {
            label = getActionDescriptionText(currentState);
        } catch (IllegalStateException e) {
            label = "ERROR";
            //TODO: SOME ERROR STATE DIALOG WINDOW
        }

        return label;
    }

    public String processActionAndRedirect(ActionState state) {

        currentlyDetailedTeam = null;

        switch (state) {
            case ADD:
                currentState = state;
                currentlyManagedTeam = new TeamDTO();

                return menuBean.goToLocation(NavigationMapper.TEAM_MODULE,
                        NavigationMapper.ADD_SECTION);
            case EDIT:
                currentState = state;
                
                return menuBean.goToLocation(NavigationMapper.TEAM_MODULE,
                        NavigationMapper.EDIT_SECTION);
            case VIEW:
                currentState = state;
                currentlyManagedTeam = new TeamDTO();
                
                return menuBean.goToLocation(NavigationMapper.TEAM_MODULE,
                        NavigationMapper.VIEW_SECTION);
            default:
                System.err.println("UNHANDLED ACTION = NO REDIRECT");
        }

        return "";
    }

    public String handleAction() {

        switch (currentState) {
            case ADD:
                if (addTeam()) {
                    return menuBean.goToLocation(NavigationMapper.TEAM_MODULE,
                        NavigationMapper.VIEW_SECTION);
                }
                break;
            case EDIT:
                if (changeTeam()) {
                    return menuBean.goToLocation(NavigationMapper.TEAM_MODULE,
                        NavigationMapper.VIEW_SECTION);
                }
                break;
            case DELETE:
                removeTeam();
                break;
            default:
                System.err.println("UNHANDLED ACTION");
        }

        return "";
    }

    private boolean addTeam() {

        try {
            teamServiceBean.addTeam(currentlyManagedTeam);
        } catch (RuntimeException e) {
            return false;
        }

        return true;
    }

    private boolean changeTeam() {
        try {
            teamServiceBean.changeTeam(currentlyManagedTeam);
        } catch (RuntimeException e) {
            return false;
        }
        return true;
    }

    private boolean removeTeam() {
        try {
            teamServiceBean.removeTeam(currentlyManagedTeam);

            if (currentlyDetailedTeam != null
                && Objects.equals(currentlyManagedTeam.getId(),
                            currentlyDetailedTeam.getTeam().getId())) {

                currentlyDetailedTeam = null;
            }

        } catch (RuntimeException e) {
            return false;
        }
        return true;
    }

    public void processClickEvent(Object rowKey) {

        Long id = valueOf((String) rowKey);

        if (currentlyDetailedTeam != null
            && Objects.equals(currentlyDetailedTeam.getTeam().getId(), id)) {
            currentlyDetailedTeam = null;
        } else {
            currentlyDetailedTeam = teamServiceBean.getTeamDetail(id);
        }
    }

}
